﻿using System.Text;
using System.Web.Mvc;
using Omu.Awesome.Core;

namespace Omu.Awesome.Mvc.Helpers
{
    public static class PaginationHelpers
    {
        public static MvcHtmlString Pagination(this HtmlHelper html)
        {
            var pageable = html.ViewData.Model as IPageableInfo;
            if (pageable == null) throw new AwesomeException("your view is not strongly typed to IPageable<>, make it strongly typed to IPageable<> or specify all the parameters");
            
            var pageCount = pageable.PageCount;
            var pageIndex = pageable.PageIndex;
            return Pagination(html, pageCount, pageIndex, html.Controller(), html.Action());
        }

        public static MvcHtmlString Pagination(this HtmlHelper html, int pageCount, int pageIndex, string controller, string action)
        {
            var urlHelper = new UrlHelper(html.ViewContext.RequestContext);

            var sb = new StringBuilder();
            sb.Append("<div class='ae-pagination'>");

            if (pageCount < 8)
                sb.Append(RenderButtons(1, pageCount, pageIndex, urlHelper, controller, action));
            else if (pageIndex < 5)
                sb.AppendFormat("{0} ... {1}", RenderButtons(1, 5, pageIndex, urlHelper, controller, action), RenderButton(pageCount, pageIndex, urlHelper, controller, action));
            else if (pageIndex > pageCount - 5)
                sb.AppendFormat("{0} ... {1}", RenderButton(1, pageIndex, urlHelper, controller, action),
                                RenderButtons(pageCount - 5, pageCount, pageIndex, urlHelper, controller, action));
            else
                sb.AppendFormat("{0} ... {1} ... {2}",
                                RenderButton(1, pageIndex, urlHelper, controller, action),
                                RenderButtons(pageIndex - 2, pageIndex + 2, pageIndex, urlHelper, controller, action),
                                RenderButton(pageCount, pageIndex, urlHelper, controller, action));

            sb.Append("</div>");

            return MvcHtmlString.Create(sb.ToString());
        }
        
        private static string RenderButtons(int from, int to, int index, UrlHelper urlHelper, string controller, string action)
        {
            var s = new StringBuilder();
            for (var i = from; i <= to; i++)
            {
                s.Append(RenderButton(i, index, urlHelper, controller, action));
            }
            return s.ToString();
        }

        private static string RenderButton(int number, int index, UrlHelper urlHelper, string controller, string action)
        {
            if (index != number)
                return string.Format("<a href='{0}'>{1}</a>",
                                     urlHelper.Action(action, controller, new { page = number }), number);

            return string.Format("<a class='ae-pagination-current' href='#'>{0}</a>", number);
        }
       
    }
}